package com.example.glide_pre_demo.manager;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.target.ViewTarget;
import com.example.glide_pre_demo.MyApplication;

/**
 * @author carlos
 * @date 2016/5/15
 */
public class ImageLoader {

    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";

    public static void loadFromResource(Context context, int resId, ImageView iv) {
        Glide.with(context.getApplicationContext())
                .load(resId)
                .into(iv);
    }

    public static void loadFromResourceUri(Context context, int resId, ImageView iv) {
        Glide.with(context.getApplicationContext())
                .load(resourceIdToUri(resId))
                .into(iv);
    }

    public static void loadFromUrl(Context context, String url, ImageView iv) {
        Glide.with(context.getApplicationContext())
                .load(url)
                .into(iv);
    }

    public static void loadFromUrl(Context context, String url, Target target) {
        Glide.with(context.getApplicationContext())
                .load(url)
                .asBitmap()
                .fitCenter()
                .into(target);
    }

    public static void loadImageViewTarget(ViewTarget target, String url) {
        Glide.with(MyApplication.getAppContext())
                .load(url)
                .into(target);
    }

    private static Uri resourceIdToUri(int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + MyApplication.getAppContext().getPackageName()
                + FOREWARD_SLASH + resourceId);
    }


}
